/*************************************************************************
	> File Name: P1037.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2024-12-13 14:18:20
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

class BigInt : public vector<int> {
public:
    BigInt() {push_back(0);}
    BigInt(int x) {
        push_back(x);
        proccess_digit();
    }
    
    BigInt &operator*=(const BigInt &x) {
        BigInt ret;
        int len = size() + x.size() + 1;
        for (int i = 0; i < len; i++) ret.push_back(0);
        for (int i = 0; i < size(); i++) {
            for (int j = 0; j < x.size(); j++) {
                ret[i + j] += at(i) * x[j];
            }
        }
        *this = ret;
        proccess_digit();
        return *this;
    }

private:
    void proccess_digit() {
        for (int i = 0; i < size(); i++) {
            if (at(i) < 10) continue;
            if (i + 1 == size()) push_back(0);
            at(i + 1) += at(i) / 10;
            at(i) %= 10;
        }
        deal_zero();
        return ;
    }

    void deal_zero() {
        while (size() > 1 && back() == 0) pop_back();
    }
};

ostream &operator<<(ostream &out, const BigInt &a) {
    for (int i = a.size() - 1; i >= 0; i--) {
        out << a[i];
    }
    return out;
}

int dis[10][10];
int check[50];
int cnt[10];

int main() {
    string str;
    int k;
    cin >> str >> k;
    for (int i = 0; i < str.size(); i++) {
        check[str[i] - '0'] = 1;
    }
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        dis[a][b] = 1;
    }
    //弗洛伊德算法
    for (int k = 0; k <= 9; k++) {
        for (int i = 0; i <= 9; i++) {
            for (int j = 0; j <= 9; j++) {
                if (dis[i][j] || (dis[i][k] && dis[k][j])) dis[i][j] = 1; 
            }
        }
    }
    //自己不能变回自己
    for (int i = 0; i <= 9; i++) dis[i][i] = 0;
    //根据乘法原理，开始计算
    for (int i = 0; i <= 9; i++) {
        int temp = 1;
        for (int j = 0; j <= 9; j++) {
            if (dis[i][j] && check[i]) temp += 1;
        }
        if (str[0] - '0' == i && dis[i][0]) temp -= 1;
        if (temp) cnt[i] = temp;
    }
    BigInt ans = 1;
    for (int i = 0; i < str.size(); i++) {
        int val = cnt[str[i] - '0'];
        if (val) ans *= val;
    }
    cout << ans << endl;
    return 0;
}
